<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Article extends CI_Controller {
	public function __construct(){
		parent::__construct();
		 if(!$this->session->userdata('is_login')){
            redirect("/admin/auth/login");
	}
}	
	private	$validate_config = array(
	    array(
	        'field' => 'title',
	        'label' => '文章',
	        'rules' => 'required'
	    ),
	    array(
	        'field' => 'desc',
	        'label' => '摘要',
	        'rules' => 'required'
	        ),
	    array(
	        'field' => 'img',
	        'label' => '图片',
	        'rules' => 'required'
	    ),
	    array(
	        'field' => 'catalog_id',
	        'label' => '分类',
	        'rules' => 'required'
	    )
	);
	public function index($page=1){
		    	$this->load->model("Article_model");
		    	$pagesize=10;
		    	$articles=$this->Article_model->get_by_page($page,$pagesize);
		    	$data=array(
		    		"articles"=>$articles,
		    		"pagename"=>1
		    );
		    	
		    	$this->load->library('pagination');
		    	$total=$this->Article_model->count_all();
				$config['base_url'] =site_url("/admin/article/index/");
				$config['total_rows'] = $total;
				$config['per_page'] = $pagesize;
				$config['use_page_numbers'] = TRUE;
				
			
				$this->pagination->initialize($config);
				$this->load->view('admin/article_index',$data);
	}
		public function add()
		{	
			$this->load->library('form_validation');
			
	       	$this->form_validation->set_rules($this->validate_config);
	        if ($this->form_validation->run() == FALSE)
	        {	
	        	$article=new stdClass();
	        	$article->title="";
	        	$article->desc="";
	        	$article->img="";
	        	$article->content="";
	        	$article->catalog_id="";
	        	$this->load->model('Catalog_model');
	        	$data=array(
	        		"catalog"=>$this->Catalog_model->get_all(),4
	        		'title'=>"添加",
	        		'article'=>$article,
	        		);
	            $this->load->view('admin/article_form',$data);
	        }
	        else
	        {	
	        	$data=$this->input->post();
	        	$this->load-> model("Article_model");
	        	$this->Article_model->add_one($data);
	        	

	            redirect("admin/article/index");
	        }
	    } 
	    public function recommend($id)
	    	{	
				$this->load->model("Article_model");
				$this->Article_model->recommend($id);
				
				// redirect("admin/article/index");
	    	}
	    	public function edit($id)
		{	
			$this->load->library('form_validation');
			
	       $this->form_validation->set_rules($this->validate_config);
	        if ($this->form_validation->run() == FALSE)
	        {	
	        	$this->load->model('Catalog_model');
	        	$this->load->model('Article_model');
	        	$article=$this->Article_model->get_one($id);
	        	$data=array(
	        		"catalog"=>$this->Catalog_model->get_all(),
	        		'article'=>$article,
	        		'title'=>"编辑"
	        		);
	            $this->load->view('admin/article_form',$data);
	        }
	        else
	        {	
	        	$data=$this->input->post();
	        	$this->load-> model("Article_model");
	        	$this->Article_model->edit_one($id,$data);
	            redirect("admin/article/index");
	        }
	    }
	    	public function delete($id)
	    {
	    	$this->load->model("Article_model");
	    	$this->Article_model->delete_one($id);
	    	redirect("admin/article/index");

	    } 
}
 